package core

import (
	"sync"
)

var (
	// sourceConnectorRegistry 是源连接器注册表
	sourceConnectorRegistry = NewConnectorRegistryImpl()
	// sinkConnectorRegistry 是接收器连接器注册表
	sinkConnectorRegistry = NewConnectorRegistryImpl()
	// processorRegistry 是处理器注册表
	processorRegistry = NewProcessorRegistryImpl()
	// registryMu 是保护注册表的互斥锁
	registryMu sync.RWMutex
)

// RegisterSourceConnector 注册源连接器
func RegisterSourceConnector(name string, factory SourceConnectorFactory) {
	registryMu.Lock()
	defer registryMu.Unlock()
	sourceConnectorRegistry.RegisterSourceConnector(name, factory)
}

// RegisterSinkConnector 注册接收器连接器
func RegisterSinkConnector(name string, factory SinkConnectorFactory) {
	registryMu.Lock()
	defer registryMu.Unlock()
	sinkConnectorRegistry.RegisterSinkConnector(name, factory)
}

// RegisterProcessor 注册处理器
func RegisterProcessor(name string, factory ProcessorFactory) {
	registryMu.Lock()
	defer registryMu.Unlock()
	processorRegistry.RegisterProcessor(name, factory)
}

// GetSourceConnector 获取源连接器
func GetSourceConnector(name string) (SourceConnector, error) {
	registryMu.RLock()
	defer registryMu.RUnlock()
	return sourceConnectorRegistry.GetSourceConnector(name)
}

// GetSinkConnector 获取接收器连接器
func GetSinkConnector(name string) (SinkConnector, error) {
	registryMu.RLock()
	defer registryMu.RUnlock()
	return sinkConnectorRegistry.GetSinkConnector(name)
}

// GetProcessor 获取处理器
func GetProcessor(name string) (Processor, error) {
	registryMu.RLock()
	defer registryMu.RUnlock()
	return processorRegistry.GetProcessor(name)
}

// ListSourceConnectors 列出源连接器
func ListSourceConnectors() []string {
	registryMu.RLock()
	defer registryMu.RUnlock()
	return sourceConnectorRegistry.ListSourceConnectors()
}

// ListSinkConnectors 列出接收器连接器
func ListSinkConnectors() []string {
	registryMu.RLock()
	defer registryMu.RUnlock()
	return sinkConnectorRegistry.ListSinkConnectors()
}

// ListProcessors 列出处理器
func ListProcessors() []string {
	registryMu.RLock()
	defer registryMu.RUnlock()
	return processorRegistry.ListProcessors()
}

// ConnectorRegistryImpl 是ConnectorRegistry的实现
type ConnectorRegistryImpl struct {
	// sourceConnectors 是源连接器工厂
	sourceConnectors map[string]SourceConnectorFactory
	// sinkConnectors 是接收器连接器工厂
	sinkConnectors map[string]SinkConnectorFactory
	// mu 是保护连接器的互斥锁
	mu sync.RWMutex
}

// NewConnectorRegistryImpl 创建一个新的连接器注册表实现
func NewConnectorRegistryImpl() *ConnectorRegistryImpl {
	return &ConnectorRegistryImpl{
		sourceConnectors: make(map[string]SourceConnectorFactory),
		sinkConnectors:   make(map[string]SinkConnectorFactory),
	}
}

// RegisterSourceConnector 注册源连接器
func (r *ConnectorRegistryImpl) RegisterSourceConnector(name string, factory SourceConnectorFactory) {
	r.mu.Lock()
	defer r.mu.Unlock()
	r.sourceConnectors[name] = factory
}

// RegisterSinkConnector 注册接收器连接器
func (r *ConnectorRegistryImpl) RegisterSinkConnector(name string, factory SinkConnectorFactory) {
	r.mu.Lock()
	defer r.mu.Unlock()
	r.sinkConnectors[name] = factory
}

// GetSourceConnector 获取源连接器
func (r *ConnectorRegistryImpl) GetSourceConnector(name string) (SourceConnector, error) {
	r.mu.RLock()
	defer r.mu.RUnlock()
	factory, ok := r.sourceConnectors[name]
	if !ok {
		return nil, fmt.Errorf("source connector not found: %s", name)
	}
	return factory()
}

// GetSinkConnector 获取接收器连接器
func (r *ConnectorRegistryImpl) GetSinkConnector(name string) (SinkConnector, error) {
	r.mu.RLock()
	defer r.mu.RUnlock()
	factory, ok := r.sinkConnectors[name]
	if !ok {
		return nil, fmt.Errorf("sink connector not found: %s", name)
	}
	return factory()
}

// ListSourceConnectors 列出源连接器
func (r *ConnectorRegistryImpl) ListSourceConnectors() []string {
	r.mu.RLock()
	defer r.mu.RUnlock()
	connectors := make([]string, 0, len(r.sourceConnectors))
	for name := range r.sourceConnectors {
		connectors = append(connectors, name)
	}
	return connectors
}

// ListSinkConnectors 列出接收器连接器
func (r *ConnectorRegistryImpl) ListSinkConnectors() []string {
	r.mu.RLock()
	defer r.mu.RUnlock()
	connectors := make([]string, 0, len(r.sinkConnectors))
	for name := range r.sinkConnectors {
		connectors = append(connectors, name)
	}
	return connectors
}

// ProcessorRegistryImpl 是ProcessorRegistry的实现
type ProcessorRegistryImpl struct {
	// processors 是处理器工厂
	processors map[string]ProcessorFactory
	// mu 是保护处理器的互斥锁
	mu sync.RWMutex
}

// NewProcessorRegistryImpl 创建一个新的处理器注册表实现
func NewProcessorRegistryImpl() *ProcessorRegistryImpl {
	return &ProcessorRegistryImpl{
		processors: make(map[string]ProcessorFactory),
	}
}

// RegisterProcessor 注册处理器
func (r *ProcessorRegistryImpl) RegisterProcessor(name string, factory ProcessorFactory) {
	r.mu.Lock()
	defer r.mu.Unlock()
	r.processors[name] = factory
}

// GetProcessor 获取处理器
func (r *ProcessorRegistryImpl) GetProcessor(name string) (Processor, error) {
	r.mu.RLock()
	defer r.mu.RUnlock()
	factory, ok := r.processors[name]
	if !ok {
		return nil, fmt.Errorf("processor not found: %s", name)
	}
	return factory()
}

// ListProcessors 列出处理器
func (r *ProcessorRegistryImpl) ListProcessors() []string {
	r.mu.RLock()
	defer r.mu.RUnlock()
	processors := make([]string, 0, len(r.processors))
	for name := range r.processors {
		processors = append(processors, name)
	}
	return processors
}
